﻿using soma.Models;
using soma.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Mvc;

namespace soma.Controllers
{
    public class RegisterController : Controller
    {
        //
        // GET: /Register/

        public ActionResult Index()
        {
            ViewData["errorMessage"] = "alabala";

            return View();
        }

        //
        // GET: /Register/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        [HttpPost]
        public HttpResponseMessage Create(Email email)
        {
         //   Console.WriteLine(email.UserEmail);
            HttpResponseMessage responseMessage = new HttpResponseMessage();

            try
            {
                if (email.UserEmail == null || email.UserEmail.Equals(string.Empty))
                {
                    responseMessage.StatusCode = System.Net.HttpStatusCode.BadRequest;
                    responseMessage.ReasonPhrase = "A valid email address must be provided";
                    //         return RedirectToAction("Index");

                    return responseMessage;

                }

                string errorMessage;
                if (!APIKeyVerifier.Instance.CreateKey(email.UserEmail, out errorMessage))
                {
                    responseMessage.StatusCode = System.Net.HttpStatusCode.BadRequest;
                    responseMessage.ReasonPhrase = errorMessage;

                    return responseMessage;
                    //                return RedirectToAction("Index");
                }
            }
            catch
            {
                responseMessage.StatusCode = System.Net.HttpStatusCode.BadRequest;
                responseMessage.ReasonPhrase = "An error has occured. Please try again!";
                //             return RedirectToAction("Index");

                return responseMessage;
            }

            responseMessage.StatusCode = System.Net.HttpStatusCode.OK;
            responseMessage.ReasonPhrase = "The key was succesfully generated and was emailed to the specified account";

            return responseMessage;
        }
        /*
        [HttpPost]
        public void Create(FormCollection collection)
        {
            HttpResponseMessage responseMessage = new HttpResponseMessage();

            try
            {
           //     ViewBag.Exception = "sdasdasadas";
              //  return View("Index");
                string key = collection["user_email"] as string;

                if (key == null || key.Equals(string.Empty))
                {
                    responseMessage.StatusCode = System.Net.HttpStatusCode.BadRequest;
                    responseMessage.ReasonPhrase = "A valid email address must be provided";
           //         return RedirectToAction("Index");

                }

                string errorMessage;
                if (!APIKeyVerifier.Instance.CreateKey(key, out errorMessage))
                {
                    ViewData["errorMessage"] = errorMessage;
    //                return RedirectToAction("Index");
                }
            }
            catch
            {
                responseMessage.StatusCode = System.Net.HttpStatusCode.BadRequest;
                responseMessage.ReasonPhrase = "An error has occured. Please try again!";
   //             return RedirectToAction("Index");
            }

   //         return RedirectToAction("Index");
        }
        */
        //
        // GET: /Register/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Register/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Register/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Register/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
